<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="78px"
    >
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="88px" label="用户手机号" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入用户手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="购买时间" prop="dateRange">
        <el-date-picker
          v-model="queryParams.dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="可用次数" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入可用次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆类型" prop="carModel">
        <el-select
          style="width: 100%"
          v-model="form.carModel"
          placeholder="请选择车辆类型"
        >
          <el-option
            v-for="dict in dict.type.srv_car_model"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['service:cardSendRecord:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="cardSendRecordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="用户昵称" align="center" prop="nickName" />
      <el-table-column label="用户手机号" align="center" prop="phonenumber" />
      <el-table-column label="卡名称" align="center" prop="name" />
      <el-table-column label="车辆类型" align="center" prop="carModel">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.srv_card_type"
            :value="scope.row.carModel"
          />
        </template>
      </el-table-column>
      <el-table-column label="购买时间" align="center" prop="createTime" />
      <el-table-column label="可用次数" align="center" prop="number" />
      <el-table-column label="总次数" align="center" prop="totalNumber" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <!--                <el-button-->
          <!--                  size="mini"-->
          <!--                  type="text"-->
          <!--                  icon="el-icon-edit"-->
          <!--                  @click="handleUpdate(scope.row)"-->
          <!--                  v-hasPermi="['service:cardSendRecord:edit']"-->
          <!--                  >修改</el-button-->
          <!--                >-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:cardSendRecord:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员卡发放记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="会员卡配置id" prop="configId">
          <el-input v-model="form.configId" placeholder="请输入会员卡配置id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户手机号" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入用户手机号" />
        </el-form-item>
        <el-form-item label="卡名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入卡名称" />
        </el-form-item>
        <el-form-item label="卡介绍" prop="introduction">
          <el-input v-model="form.introduction" placeholder="请输入卡介绍" />
        </el-form-item>
        <el-form-item label="车辆型号" prop="carModel">
          <el-input v-model="form.carModel" placeholder="请输入车辆型号" />
        </el-form-item>
        <el-form-item label="用户车辆id" prop="userCarId">
          <el-input v-model="form.userCarId" placeholder="请输入用户车辆id" />
        </el-form-item>
        <el-form-item label="使用时间" prop="useTime">
          <el-input v-model="form.useTime" placeholder="请输入使用时间" />
        </el-form-item>
        <el-form-item label="生效开始日期" prop="startDate">
          <el-date-picker
            clearable
            v-model="form.startDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择生效开始日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生效结束日期" prop="endDate">
          <el-date-picker
            clearable
            v-model="form.endDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择生效结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="可用次数" prop="number">
          <el-input v-model="form.number" placeholder="请输入可用次数" />
        </el-form-item>
        <el-form-item label="延期次数" prop="delayNumber">
          <el-input v-model="form.delayNumber" placeholder="请输入延期次数" />
        </el-form-item>
        <el-form-item label="支付信息" prop="payInfo">
          <el-input
            v-model="form.payInfo"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCardSendRecord,
  getCardSendRecord,
  delCardSendRecord,
  addCardSendRecord,
  updateCardSendRecord,
} from "@/api/service/cardSendRecord";
import { omit } from "lodash/object";

export default {
  name: "CardSendRecord",
  dicts: ["srv_car_model"],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员卡发放记录表格数据
      cardSendRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dateRange: [],
        configId: undefined,
        userId: undefined,
        nickName: undefined,
        phonenumber: undefined,
        name: undefined,
        introduction: undefined,
        type: undefined,
        dictType: undefined,
        carModel: undefined,
        userCarId: undefined,
        useTime: undefined,
        startDate: undefined,
        endDate: undefined,
        number: undefined,
        delayNumber: undefined,
        serveId: undefined,
        serveName: undefined,
        expiredStatus: undefined,
        payStatus: undefined,
        payInfo: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: "id不能为空", trigger: "blur" }],
        configId: [
          { required: true, message: "会员卡配置id不能为空", trigger: "blur" },
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" },
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        phonenumber: [
          { required: true, message: "用户手机号不能为空", trigger: "blur" },
        ],
        name: [{ required: true, message: "卡名称不能为空", trigger: "blur" }],
        introduction: [
          { required: true, message: "卡介绍不能为空", trigger: "blur" },
        ],
        type: [
          { required: true, message: "卡类型不能为空", trigger: "change" },
        ],
        dictType: [
          {
            required: true,
            message: "字典配置类型不能为空",
            trigger: "change",
          },
        ],
        carModel: [
          { required: true, message: "车辆型号不能为空", trigger: "blur" },
        ],
        userCarId: [
          { required: true, message: "用户车辆id不能为空", trigger: "blur" },
        ],
        useTime: [
          { required: true, message: "使用时间不能为空", trigger: "blur" },
        ],
        startDate: [
          { required: true, message: "生效开始日期不能为空", trigger: "blur" },
        ],
        endDate: [
          { required: true, message: "生效结束日期不能为空", trigger: "blur" },
        ],
        number: [
          { required: true, message: "可用次数不能为空", trigger: "blur" },
        ],
        delayNumber: [
          { required: true, message: "延期次数不能为空", trigger: "blur" },
        ],
        serveId: [
          { required: true, message: "服务id不能为空", trigger: "change" },
        ],
        serveName: [
          { required: true, message: "服务名称不能为空", trigger: "change" },
        ],
        payStatus: [
          {
            required: true,
            message: "现金支付状态不能为空",
            trigger: "change",
          },
        ],
        payInfo: [
          { required: true, message: "支付信息不能为空", trigger: "blur" },
        ],
        remark: [{ required: true, message: "备注不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员卡发放记录列表 */
    getList() {
      this.loading = true;
      const { dateRange } = this.queryParams;
      const [beginTime, endTime] = dateRange || [];
      listCardSendRecord({
        ...omit(this.queryParams, "dateRange"),
        params: { beginTime, endTime },
        type: "0",
      }).then((response) => {
        this.cardSendRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        configId: undefined,
        userId: undefined,
        nickName: undefined,
        phonenumber: undefined,
        name: undefined,
        introduction: undefined,
        type: undefined,
        dictType: undefined,
        carModel: undefined,
        userCarId: undefined,
        useTime: undefined,
        startDate: undefined,
        endDate: undefined,
        number: undefined,
        delayNumber: undefined,
        serveId: undefined,
        serveName: undefined,
        expiredStatus: undefined,
        payStatus: undefined,
        payInfo: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员卡发放记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getCardSendRecord(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改会员卡发放记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateCardSendRecord(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addCardSendRecord(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除会员卡发放记录编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delCardSendRecord(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "service/cardSendRecord/export",
        {
          ...this.queryParams,
        },
        `cardSendRecord_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
